﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Task_Network_Center.General;
using System.Data;

namespace Task_Network_Center
{
    /// <summary>
    /// Logique d'interaction pour FrmAddTask.xaml
    /// </summary>
    public partial class FrmModifTask : Window
    {
       public object[,] TempDepartementArray;
       public object[,] TempEmployeArray;
       public object[,] TempPrioriteArray;
       public object[,] TempEquipeArray;

       public int lastValue = 0;
       public int id;

       public FrmModifTask()
        {
            InitializeComponent();
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            GCGDataBaseTableLib.TaskTable task = new GCGDataBaseTableLib.TaskTable();
            string query = "SELECT * FROM tablelistdestaches_general WHERE id_tache = " + GCGGeneralLib.GeneralVar.CurrentSelectedCell;
            DataTable tb = task.QueryTableReadable(query, TempDepartementArray, TempEmployeArray, TempEquipeArray, TempPrioriteArray);
            updateCbo();
            

            txtProjets.Text = tb.Rows[0][1].ToString();
            txtDescription.Text = tb.Rows[0][2].ToString();

            if (tb.Rows[0][3].ToString() != "")
            {


                for (int i = 0; i < TempEmployeArray.GetLength(1); i++)
                {
                    if (tb.Rows[0][3].ToString() == TempEmployeArray[1, i].ToString())
                    {
                        cboInscritPar.SelectedIndex = Convert.ToInt32(TempEmployeArray[3, i]);
                        break;
                    }
                }
            }
            else
            {
                cboInscritPar.SelectedIndex = -1;
            }
            datePickerInscription.Text = tb.Rows[0][4].ToString();
            txtSolution.Text = tb.Rows[0][5].ToString();


            if (tb.Rows[0][6].ToString() != "")
            {
                for (int i = 0; i < TempEmployeArray.GetLength(1); i++)
                {
                    if (tb.Rows[0][6].ToString() == TempEmployeArray[1, i].ToString())
                    {
                        cboExecutant.SelectedIndex = Convert.ToInt32(TempEmployeArray[3, i]);
                        break;
                    }
                }
            }
            else
            {
                cboExecutant.SelectedIndex = -1;
            }
            txtDuree.Text = tb.Rows[0][7].ToString();
            txtProgression.Text = tb.Rows[0][8].ToString();
            txtAvancement.Text = tb.Rows[0][9].ToString();


            if (tb.Rows[0][10].ToString() != "")
            {
                for (int i = 0; i < TempEquipeArray.GetLength(1); i++)
                {
                    if (tb.Rows[0][10].ToString() == TempEquipeArray[1, i].ToString())
                    {
                        cboEquipe.SelectedIndex = Convert.ToInt32(TempEquipeArray[2, i]);
                        break;
                    }
                }
            }
            else
            {
                cboEquipe.SelectedIndex = -1;
            }

            if (tb.Rows[0][11].ToString() != "")
            {
                for (int i = 0; i < TempPrioriteArray.GetLength(1); i++)
                {
                    if (tb.Rows[0][11].ToString() == TempPrioriteArray[1, i].ToString())
                    {
                        cboPriorite.SelectedIndex = Convert.ToInt32(TempPrioriteArray[2, i]);
                        break;
                    }
                }
            }
            else
            {
                cboPriorite.SelectedIndex = -1;
            }

            datePickerEcheance.Text = tb.Rows[0][12].ToString();


            if (tb.Rows[0][13].ToString() != "")
            {
                for (int i = 0; i < TempDepartementArray.GetLength(1); i++)
                {
                    if (tb.Rows[0][13].ToString() == TempDepartementArray[1, i].ToString())
                    {
                        cboDepartement.SelectedIndex = Convert.ToInt32(TempDepartementArray[2, i]);
                        break;
                    }
                }
            }
            else
            {
                cboDepartement.SelectedIndex = -1;
            }

            datePickerCompleteLe.Text = tb.Rows[0][14].ToString();



                if (tb.Rows[0][15].ToString() != "")
                {
                    for (int i = 0; i < TempEmployeArray.GetLength(1); i++)
                    {
                        if (tb.Rows[0][15].ToString() == TempEmployeArray[1, i].ToString())
                        {
                            cboComplete.SelectedIndex = Convert.ToInt32(TempEmployeArray[3, i]);
                            break;
                        }
                    }
                }
                else
                {
                    cboComplete.SelectedIndex = -1;
                }

                txtNote1.Text = tb.Rows[0][16].ToString();
                txtNote2.Text = tb.Rows[0][17].ToString();
                txtNote3.Text = tb.Rows[0][18].ToString();
                txtNote4.Text = tb.Rows[0][19].ToString();

            
        }
        private void updateCbo()
        {
            for (int i = 0; i < TempEmployeArray.GetLength(1); i++)
            {
                cboInscritPar.Items.Add(TempEmployeArray[1, i].ToString());
                cboExecutant.Items.Add(TempEmployeArray[1, i].ToString());
                cboComplete.Items.Add(TempEmployeArray[1, i].ToString()); 
      
                
            }

            for (int i = 0; i < TempEquipeArray.GetLength(1); i++)
            {
                cboEquipe.Items.Add(TempEquipeArray[1, i].ToString());
            }

            for (int i = 0; i < TempDepartementArray.GetLength(1); i++)
            {
                cboDepartement.Items.Add(TempDepartementArray[1, i].ToString());
            }

            for (int i = 0; i < TempPrioriteArray.GetLength(1); i++)
            {
                cboPriorite.Items.Add(TempPrioriteArray[1, i].ToString());
            }
        }

        private void cmdModif_Click(object sender, RoutedEventArgs e)
        {
            if (txtProjets.Text != "" && txtDescription.Text != "" && cboExecutant.SelectedIndex != -1 && cboInscritPar.SelectedIndex != -1)
            {
                try
                {
                    decimal duree = Convert.ToDecimal(txtDuree.Text);
                    decimal progression = Convert.ToDecimal(txtProgression.Text);
                    GCGDataBaseTableLib.TaskTable TTask = new GCGDataBaseTableLib.TaskTable();
                    TTask.UpdateRow(GCGGeneralLib.GeneralVar.CurrentSelectedCell, txtProjets, txtDescription, cboInscritPar, datePickerInscription, txtSolution,
                        cboExecutant, txtDuree, txtProgression, txtAvancement, cboEquipe, cboPriorite, datePickerEcheance,
                        cboDepartement, datePickerCompleteLe, cboComplete, txtNote1, txtNote2, txtNote3, txtNote4, TempDepartementArray, TempEmployeArray, TempEquipeArray, TempPrioriteArray);

                    if (chkSendEmail.IsChecked == true)
                    {
                        string executantEmail = "";
                        for (int i = 0; i < TempEmployeArray.GetLength(1); i++)
                        {
                            if (TempEmployeArray[1, i].ToString() == cboExecutant.Text)
                            {
                                executantEmail = TempEmployeArray[2, i].ToString();
                                break;
                            }
                        }
                        if (executantEmail != "")
                        {
                            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                            message.To.Add(executantEmail);
                            message.Subject = txtProjets.Text + " - GCG Rappel de Notification";
                            message.From = new System.Net.Mail.MailAddress("info@genik.qc.ca");
                            message.Body = "Projet: " + txtProjets.Text + "\nDescription: " + txtDescription.Text;
                            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("mail.genik.qc.ca", 1025);
                            smtp.Credentials = new System.Net.NetworkCredential("info.genik.qc.ca", "40392gen");
                            smtp.Send(message);
                        }
                        else
                        {
                            MessageBox.Show("L'exécutant que vous avez choisi n'a pas d'adresse courriel configurée.");
                        }
                    }


                    if (chkSendEmailInscrivant.IsChecked == true)
                    {
                        string InscrivantEmail = "";
                        for (int i = 0; i < TempEmployeArray.GetLength(1); i++)
                        {
                            if (TempEmployeArray[1, i].ToString() == cboInscritPar.Text)
                            {
                                InscrivantEmail = TempEmployeArray[2, i].ToString();
                                break;
                            }
                        }
                        if (InscrivantEmail != "" && datePickerCompleteLe.Text == "")
                        {
                            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                            message.To.Add(InscrivantEmail);
                            message.Subject = txtProjets.Text + " - GCG Rappel de Tâche";
                            message.From = new System.Net.Mail.MailAddress("info@genik.qc.ca");
                            message.Body = "Projet: " + txtProjets.Text + "\nDescription: " + txtDescription.Text;
                            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("mail.genik.qc.ca", 1025);
                            smtp.Credentials = new System.Net.NetworkCredential("info.genik.qc.ca", "40392gen");
                            smtp.Send(message);
                        }
                        else if (InscrivantEmail != "" && datePickerCompleteLe.Text != "")
                        {
                            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                            message.To.Add(InscrivantEmail);
                            message.Subject = txtProjets.Text + " - GCG Tâche Complétée";
                            message.From = new System.Net.Mail.MailAddress("info@genik.qc.ca");
                            message.Body = "Projet: " + txtProjets.Text + "\nDescription: " + txtDescription.Text;
                            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("mail.genik.qc.ca", 1025);
                            smtp.Credentials = new System.Net.NetworkCredential("info.genik.qc.ca", "40392gen");
                            smtp.Send(message);
                        }
                        else
                        {
                            MessageBox.Show("L'inscrivant actuel n'a pas d'adresse courriel configurée.");
                        }
                    }

                    this.Close();
                }
                catch (Exception)
                {

                    MessageBox.Show("Veuiller entrer une durée ou une progression valide.");
                }
            }
            else
            {
                MessageBox.Show("La tâche doit avoir aux minimum: Projets, Inscrit Par, Description et un Exécutant");
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void datePickerCompleteLe_CalendarClosed(object sender, RoutedEventArgs e)
        {
            if (datePickerCompleteLe.Text != "")
            {
                if (cboComplete.Text == "")
                {
                    for (int i = 0; i < TempEmployeArray.GetLength(1); i++)
                    {
                        if (Convert.ToInt32(TempEmployeArray[0, i]) == GCGGeneralLib.GeneralVar.initialID)
                        {
                            cboComplete.SelectedIndex = Convert.ToInt32(TempEmployeArray[3, i]);
                            break;
                        }
                    }
                }
                lastValue = Convert.ToInt32(txtProgression.Text);
                txtProgression.Text = "100";
            }
            else
            {
                cboComplete.SelectedIndex = -1;
            }
        }

        private void datePickerCompleteLe_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }

        private void cboComplete_DropDownClosed(object sender, EventArgs e)
        {
            if (cboComplete.Text != "")
            {
                if (datePickerCompleteLe.Text == "")
                {
                    datePickerCompleteLe.Text = DateTime.Today.ToShortDateString();
                }
                
                lastValue = Convert.ToInt32(txtProgression.Text);
                txtProgression.Text = "100";
            }
        }

        private void datePickerCompleteLe_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Back || e.Key == Key.Delete)
            {
                if (datePickerCompleteLe.Text == "")
                {
                    cboComplete.SelectedIndex = -1;
                    txtProgression.Text = lastValue.ToString();
                }
            }
        }

        private void txtProgression_LostFocus(object sender, RoutedEventArgs e)
        {
            if (txtProgression.Text == "100")
            {
                    MessageBoxResult resultat = System.Windows.MessageBox.Show("Voulez-vous completé la tâche ?", "ATTENTION", MessageBoxButton.YesNo);
                    if (resultat == MessageBoxResult.Yes)
                    {
                        datePickerCompleteLe.Text = DateTime.Today.ToShortDateString();
                        
                        for (int i = 0; i < TempEmployeArray.GetLength(1); i++)
                        {
                            if (Convert.ToInt32(TempEmployeArray[0, i]) == GCGGeneralLib.GeneralVar.initialID)
                            {
                                cboComplete.SelectedIndex = Convert.ToInt32(TempEmployeArray[3, i]);
                                break;
                            }
                        }
                        
                    }
            }
        }

        private void txtProgression_GotFocus(object sender, RoutedEventArgs e)
        {
            lastValue = Convert.ToInt32(txtProgression.Text);
        }

        private void lblClearEquipe_MouseEnter(object sender, MouseEventArgs e)
        {
            lblClearEquipe.Foreground = Brushes.Red;
        }

        private void lblClearEquipe_MouseLeave(object sender, MouseEventArgs e)
        {
            lblClearEquipe.Foreground = Brushes.Black;
        }

        private void lblClearPriorite_MouseEnter(object sender, MouseEventArgs e)
        {
            lblClearPriorite.Foreground = Brushes.Red;
        }

        private void lblClearPriorite_MouseLeave(object sender, MouseEventArgs e)
        {
            lblClearPriorite.Foreground = Brushes.Black;
        }

        private void lblClearEcheance_MouseEnter(object sender, MouseEventArgs e)
        {
            lblClearEcheance.Foreground = Brushes.Red;
        }

        private void lblClearEcheance_MouseLeave(object sender, MouseEventArgs e)
        {
            lblClearEcheance.Foreground = Brushes.Black;
        }

        private void lblClearDepartement_MouseEnter(object sender, MouseEventArgs e)
        {
            lblClearDepartement.Foreground = Brushes.Red;
        }

        private void lblClearDepartement_MouseLeave(object sender, MouseEventArgs e)
        {
            lblClearDepartement.Foreground = Brushes.Black;
        }

        private void lblClearCompleteLe_MouseEnter(object sender, MouseEventArgs e)
        {
            lblClearCompleteLe.Foreground = Brushes.Red;
        }

        private void lblClearCompleteLe_MouseLeave(object sender, MouseEventArgs e)
        {
            lblClearCompleteLe.Foreground = Brushes.Black;
        }

        private void lblClearCompletePar_MouseEnter(object sender, MouseEventArgs e)
        {
            lblClearCompletePar.Foreground = Brushes.Red;
        }

        private void lblClearCompletePar_MouseLeave(object sender, MouseEventArgs e)
        {
            lblClearCompletePar.Foreground = Brushes.Black;
        }

        private void lblClearEquipe_MouseUp(object sender, MouseButtonEventArgs e)
        {
            cboEquipe.SelectedIndex = -1;
        }

        private void lblClearPriorite_MouseUp(object sender, MouseButtonEventArgs e)
        {
            cboPriorite.SelectedIndex = -1;
        }

        private void lblClearEcheance_MouseUp(object sender, MouseButtonEventArgs e)
        {
            datePickerEcheance.SelectedDate = null;
        }

        private void lblClearDepartement_MouseUp(object sender, MouseButtonEventArgs e)
        {
            cboDepartement.SelectedIndex = -1;
        }

        private void lblClearCompleteLe_MouseUp(object sender, MouseButtonEventArgs e)
        {
            datePickerCompleteLe.SelectedDate = null;
        }

        private void lblClearCompletePar_MouseUp(object sender, MouseButtonEventArgs e)
        {
            cboComplete.SelectedIndex = -1;
        }
    }
}
